package com.asia.algorithmcode.array;

import java.util.Arrays;

/**
 * @Num: 977. 有序数组的平方
 * @Addr: https://leetcode.cn/problems/squares-of-a-sorted-array/description/
 * @Type: easy
 * @Author: wanfu
 * @Date: 2024/7/26 星期五 17:00
 * @Desc: 双指针+额外的数据来 换取更优秀的时间复杂度
 */
class Solution {

    /**
     *
     * @param nums
     * @return
     */
    public int[] sortedSquares(int[] nums) {
        int start = 0;
        int end = nums.length - 1;

        int[] result = new int[nums.length];
        int index = nums.length - 1;
        while (start <= end) {
            if (nums[start] * nums[start] > nums[end] * nums[end]) {
                result[index--] = nums[start] * nums[start];
                ++start;
            } else {
                result[index--] = nums[end] * nums[end];
                --end;
            }
        }
        return result;
    }


    private int[] forces(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            nums[i] = Math.abs(nums[i]);
        }

        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            nums[i] *= nums[i];
        }
        return nums;
    }

}
